package com.myhome.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.myhome.entity.*;
import com.myhome.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;
    @Autowired
    IDepartmentService departmentService;
    @Autowired
    IPositionService positionService;
    @Autowired
    IPoliticsStatusService politicsStatusService;
    @Autowired
    INationService nationService;
    @Autowired
    IJoblevelService joblevelService;

    @ApiOperation("获取分页员工信息")
    @GetMapping("/getwithpages")
    public RespPageBean getEmployeeWithPages(@RequestParam(defaultValue = "10") Integer size, @RequestParam(defaultValue = "1") Integer currentPage, Employee employee
            , LocalDate[] timeScope) {
        return employeeService.getEmployeeWithPages(currentPage, size, employee, timeScope);
    }

    @ApiOperation(value = "获取所有部门")
    @GetMapping("/getalldepartment")
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }

    @ApiOperation(value = "获取所有政治面貌")
    @GetMapping("/getallpoliticsstatus")
    public List<PoliticsStatus> getAllPoliticsStatus() {
        return politicsStatusService.list();
    }

    @ApiOperation(value = "获取所有名族")
    @GetMapping("/getallnation")
    public List<Nation> getAllNation() {
        return nationService.list();
    }

    @ApiOperation(value = "获取所有职位")
    @GetMapping("/getalldeposition")
    public List<Position> getAllPosition() {
        return positionService.list();
    }

    @ApiOperation(value = "获取工号")
    @GetMapping("/getmaxid")
    public RespBean getMaxId() {
        return employeeService.getMaxId();
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("/addemployee")
    public RespBean addEmployee(@RequestBody Employee employee) {
        return employeeService.addEmp(employee);
    }

    @ApiOperation(value = "员工的更新")
    @PutMapping("/update")
    public RespBean updateEmployee(Employee e) {
        if (employeeService.updateById(e)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "员工删除")
    @DeleteMapping("/delete/{id}")
    public RespBean deleteEmployee(@PathVariable("id") Integer id) {
        if (employeeService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "导出员工信息")
    @GetMapping(value = "/getEmployee",produces = "application/octet-stream")
    public void getEmployee(HttpServletResponse response){
        List<Employee> result = employeeService.getEmployee(null);
        ExportParams exportParams = new ExportParams("员工表", "员工表", ExcelType.HSSF);
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, Employee.class, result);
        ServletOutputStream outputStream = null;
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("员工表.xls", "UTF-8"));
            outputStream = response.getOutputStream();
            sheets.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
